//
//  ViewController.m
//  read
//
//  Created by 宋 on 15/12/10.
//  Copyright © 2015年 宋. All rights reserved.
//

#import "ViewController.h"
#import "readViewController.h"
@interface ViewController ()
{
    NSArray <NSURL *> *images;
    NSArray <NSURL *> *btxt;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"小书屋";
    self.tableView.showsVerticalScrollIndicator = NO;
    NSBundle *bundle = [NSBundle mainBundle];
    images = [bundle URLsForResourcesWithExtension:@"jpeg" subdirectory:nil];
    btxt = [bundle URLsForResourcesWithExtension:@"txt" subdirectory:nil];
    
//警告框
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"警告" style:UIBarButtonItemStylePlain target:self action:@selector(onlin:)];
    
}  

- (void)onlin:(id)sender
{
    UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"警告⚠️" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *act = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
        
    }];
    
    [aler addAction:act];
    [self presentViewController:aler animated:YES completion:nil];
    
//    UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"是否退出" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//    [aler show];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (!images || images == 0) {
        return 0;
    }
    return (images.count-1)/3+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath.row;
    UITableViewCell *cell = [UITableViewCell new];
    NSInteger index = row * 3;
    for (int i = 0; i<3 && (index + i) < images.count; i++) {
        UIImage * image = [UIImage imageNamed:images[index + i]  .lastPathComponent];
        UIImageView * vi = [[UIImageView alloc]initWithImage:image];
        
        CGFloat edge = 15;
        CGFloat winth = (self.view.frame.size.width - edge * 4)/3;
        vi.frame = CGRectMake(edge + (edge + winth) * i, 8, winth, 139 - 8 - 8*2);
        [cell addSubview:vi];
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = vi.bounds;
         [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 100 + index + i;
        
        [vi addSubview:btn];
        vi.userInteractionEnabled = YES;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BookShelfCell"]];
    return cell;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 139;
}


- (void)onClick:(UIButton *)sender
{
    NSInteger index = sender.tag - 100;
    NSURL * imageURL = images[index];
    NSError * err ;
    NSString * bookname = imageURL.lastPathComponent;
    bookname = bookname.stringByDeletingPathExtension;
    
    bookname = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:bookname ofType:@"txt"]   encoding:NSUTF8StringEncoding error:& err];
    
    [bookname setAccessibilityNavigationStyle:UIAccessibilityNavigationStyleAutomatic];
    
    readViewController * con = [[readViewController alloc] init];
    con.text = bookname;
    
    [self.navigationController pushViewController:con animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
